$(function(){
	$("#cancel").click(function(){
		$("#add").hide();
		exit();
	})
})
/**
 * 退出清空文本框
 * @returns
 */
function exit(){
	$("#bumeng").val("");
	$("#dingdan").val("");
	$("#yijian").val("");
	$("#tg").val(2);
	load();
	//关闭所有页面层
	layer.closeAll('page');
}

layui.use(['form', 'layedit', 'laydate','table'], function(){
  var table = layui.table
  ,laydate = layui.laydate
  ,form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit;
  
  //开始时间
  var startDate=laydate.render({
      elem: '#date',//开始时间ID
      type: 'date',//时间类型
      max:0,//设置一个默认最大值(为当前时间)
      done: function(value, date){
          endDate.config.min ={
              year:date.year,
              month:date.month-1, 
              date: date.date,
          };
      }
  });
  //结束时间
  var endDate= laydate.render({
      elem: '#time',//选择器结束时间
      type: 'date',//时间类型
      max:0,//设置一个默认最大值(为当前时间)
      done: function(value,date){
          startDate.config.max={
              year:date.year,
              month:date.month-1,
              date: date.date,
          }
      }
  });
  
  //监听工具条
  table.on('tool(demo)', function(obj){
    var da = obj.data;
    if(obj.event === 'detail'){
    	layer.open({
	         type: 2	 
	        ,title: '批发订单详情'
	        ,area: ['800px', '400px']
    		,offset: 'auto'
	        ,content:['../../page/zyf/detail/wsDetail.html?whId='+da.wholeId+'','on']
	        ,end : function() {
	        	
			}
	    });
     }else if (obj.event === 'sh'){
    	 layer.open({
	         type: 1	 //此处以iframe举例
	        ,title: '批发订单审核'
	        ,offset: 'auto'
	        ,area: ['440px', '400px']
	        ,content:$('#add')
	        ,success: function(layero){
	        	$("#idss").val(da.auId)
	        	$("#ids").val(da.wholeId)
	        },end : function() {
				exit();
				$("#add").hide();
			}
	    });
     }
    /**
     * 审核订单
     */
	    $('#tijiao').on('click', function(){
	  	  if($("#yijian").val()==""){
	  		layer.msg("请输入审核意见!");
	  		return false;
	  	  }else{
	  		  var url = "../../wholeSale/auditWholesale.action";
	  		  var data = $("#myform").serialize();
	  		  $.post(url,data,function(mes){
	  			  if(mes.state==1){
	  				$("#add").hide();
	  				exit();
	  				layer.msg("审核成功!");
	  				$(".layui-laypage-btn")[0].click();//当前表格刷新
	  			  }else{
	  				layer.msg("审核失败!");
	  			  }
	  		  },"json");
	  	  }
    });
  });
  /**
   * 条件查询
   */
  var $ = layui.$, active = {
		  reload: function(){
		      var date = $('#date');
		      var time = $('#time');
		      var oper = $('#oper');
		      var state= $('#state');
		      //执行重载
		      table.reload('idTest', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	startTime: date.val(),
		        	endTime: time.val(),
		        	wholeMan: oper.val(),
		        	auState:state.val()
		        }
		      });
		}
 };
  
	$('.demoTable .layui-btn').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});
	
});
/**
 * 渲染
 * @returns
 */
function load() {
	 var form = layui.form
	 form.render();
}